package com.example.service.impl;

import com.example.exception.ImoocMallException;
import com.example.exception.ImoocMallExceptionEnum;
import com.example.mall.model.dao.CategoryMapper;
import com.example.mall.model.pojo.Category;
import com.example.mall.model.request.AddCategory;
import com.example.mall.model.request.UpdateCategory;
import com.example.mall.model.vo.CategoryVO;
import com.example.service.CategoryService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    //    添加商品分类
    @Autowired(required = false)
    CategoryMapper categoryMapper;
    @Override
    public void AddProduct(AddCategory addCategory) {
        Category category = new Category();
        BeanUtils.copyProperties(addCategory, category);
//        商品是否重名
        Category categoryOld = categoryMapper.selectProductName(addCategory.getName());
        if (categoryOld != null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.EXIT_PRODUCT);
        }
        int count = categoryMapper.insertSelective(category);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.ADD_PRODUCT);
        }
    }

    //更新商品分类
    @Override
    public void UpdateCategory(UpdateCategory updateCategory) {
        Category category = new Category();
        BeanUtils.copyProperties(updateCategory, category);
//        1.检查商品id是否存在
        Category cateId = categoryMapper.selectByPrimaryKey(updateCategory.getId());
        if (cateId == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.SHOP_ID);
        }
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_PRODUCT);
        }
    }
//删除商品分类
    @Override
    public void DeleteShopClass(Integer id) {
//        判断是否存在id
       Category category= categoryMapper.selectByPrimaryKey(id);
       if(category==null){
           throw new ImoocMallException(ImoocMallExceptionEnum.NO_EXIT_SHOP);
       }
      int count = categoryMapper.deleteByPrimaryKey(id);
       if(count==0){
           throw new ImoocMallException(ImoocMallExceptionEnum.FILED_DELETE);
       }
    }

//    查询商品分类
    @Override
    public List<CategoryVO> listForAdmin(Integer pageNum,Integer pageSize) {
//    分页
        PageHelper.startPage(pageNum,pageSize,"type,order_num");
        ArrayList<CategoryVO> categoryVOS = new ArrayList<>();
//        PageInfo pageInfo = new PageInfo(categoryList);
        ErgodicSubset(categoryVOS,0);
        return categoryVOS;
    }
////    遍历子集
public void ErgodicSubset(List<CategoryVO> categoryList, Integer parentId ){
    List<Category> c_list = categoryMapper.selectShopAll(parentId);
    if(!CollectionUtils.isEmpty(c_list)){
        for(int i=0;i<c_list.size();i++){
            Category category = c_list.get(i);
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category,categoryVO);
            categoryList.add(categoryVO);
            ErgodicSubset(categoryVO.getChildCategory(),categoryVO.getId());

        }
    }
}
}
